#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"

void STInit(Stack* st)
{
	assert(st);
	st->size = 0;
	st->top = NULL;
}
void STPush(Stack* st, STDataType x)
{
	assert(st);
	STNode* newnode = (STNode*)malloc(sizeof(STNode));
	if (newnode == NULL)
	{
		perror("malloc");
		exit(-1);
	}

	newnode->data = x;
	newnode->next = NULL;

	newnode->next = st->top;
	st->top = newnode;

	st->size++;
}

void STPop(Stack* st)
{
	assert(st);
	assert(!STEmpty(st));

	STNode* next = st->top->next;
	free(st->top);
	st->top = next;

	st->size--;
}

STDataType STTop(Stack* st)
{
	assert(st);
	assert(!STEmpty(st));

	return st->top->data;
}

int STSize(Stack* st)
{
	assert(st);

	return st->size;
}

int STEmpty(Stack* st)
{
	assert(st);

	return st->size == 0;
}

void STDestroy(Stack* st)
{
	STNode* cur = st->top;
	while (cur)
	{
		STNode* next = cur->next;
		free(cur);
		cur = next;
	}
	st->size = 0;
	st->top = NULL;
}